package com.magicsoft.share.presenter.share;

import com.magicsoft.share.base.BasePresenter;
import com.magicsoft.share.bean.my.ArticleList;
import com.magicsoft.share.bean.my.UserList;
import com.magicsoft.share.constant.APIService;
import com.magicsoft.share.constant.SPKey;
import com.magicsoft.share.rx_retrofit_network.location.retrofit.RetrofitUtil;
import com.magicsoft.share.rx_retrofit_network.location.rxandroid.LoadingTransformer;
import com.magicsoft.share.rx_retrofit_network.location.rxandroid.NetWorkTransformer;
import com.magicsoft.share.rx_retrofit_network.location.rxandroid.SimpleObserver;
import com.magicsoft.share.utils.SPUtils;
import com.magicsoft.share.view.share.ISearchView;

import io.reactivex.annotations.NonNull;

/**
 * Created by lyf on 2017/11/27/027.
 */

public class SearchPresenter extends BasePresenter<ISearchView> {

    public void findUserForHome(String keyword ,int pageNum,int pageSize){
        RetrofitUtil.getApi(APIService.class)
                .findUserForHome(SPUtils.getInstance().getLong(SPKey.ID),keyword, pageNum,pageSize)
                .compose(new NetWorkTransformer<UserList>())
                .compose(new LoadingTransformer<UserList>(mContext))
                .subscribe(new SimpleObserver<UserList>(mCompositeDisposable) {
                    @Override
                    public void call(@NonNull UserList userList) {

                        getView().findUserForHomeSuccess(userList);
                    }

                    @Override
                    public void showErrorMsg(Throwable e, String errorMsg) {
                        super.showErrorMsg(e, errorMsg);
                        getView().findUserForHomeFail();
                    }
                });
    }

    public void articleList( int pageNum, int pageSize, String keyword) {
        RetrofitUtil.getApi(APIService.class)
                .articleList(SPUtils.getInstance().getLong(SPKey.ID),null, pageNum, pageSize, null, keyword, null,null)
                .compose(new NetWorkTransformer<ArticleList>())
                .compose(new LoadingTransformer<ArticleList>(mContext))
                .subscribe(new SimpleObserver<ArticleList>(mCompositeDisposable) {
                    @Override
                    public void call(@NonNull ArticleList articleList) {
                        if (null != getView()) {
                            getView().ArticleListSuccess(articleList);
                        }

                    }

                    @Override
                    public void showErrorMsg(Throwable e, String errorMsg) {
                        super.showErrorMsg(e, errorMsg);
                        if (null != getView()) {
                            getView().ArticleListFail();
                        }

                    }
                });
    }


    public void addFollow(long otherId){
        RetrofitUtil.getApi(APIService.class).addFollow(SPUtils.getInstance().getString(SPKey.TOKEN),  SPUtils.getInstance().getLong(SPKey.ID)
                ,otherId)
                .compose(new NetWorkTransformer())
                .compose(new LoadingTransformer(mContext))
                .subscribe(new SimpleObserver(mCompositeDisposable) {
                    @Override
                    public void call(Object o) {

                        getView().followChangeSuccess();
                    }
                });
    }
    public void deleteFollow(long otherId){
        RetrofitUtil.getApi(APIService.class).deleteFollow(SPUtils.getInstance().getString(SPKey.TOKEN),  SPUtils.getInstance().getLong(SPKey.ID),otherId)
                .compose(new NetWorkTransformer())
                .compose(new LoadingTransformer(mContext))
                .subscribe(new SimpleObserver(mCompositeDisposable) {
                    @Override
                    public void call(Object o) {

                        getView().followChangeSuccess();
                    }
                });
    }
}
